package com.example.vueblog.controller;

import com.example.vueblog.common.Result;
import com.example.vueblog.service.impl.CaptchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 验证码
 *
 * @author yushunshun
 * @date 2022/7/21 1:11
 */
@RestController
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;

    @GetMapping("/verifyCode/generate")
    public Result<Map<String,String>> generateVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //设置不缓存图片
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);

        //指定生成的响应图片
        response.setContentType("image/jpeg");

        //获取输出流 生成验证码
//        ServletOutputStream out = response.getOutputStream();
//        loginService.generateVerifyCode(out);
        Map<String,String> map = captchaService.generateVerifyCode();
        return Result.ok(map);
    }
}
